#!/usr/bin/env python
# vim: set fileencoding=UTF-8

###############################################################################
#
# filename:  pkgs.py
#
# purpose:   Packages management for a config section
#
# author(s): Alessandro Molari (mola89.molarulez.com)
#            Luca Molari
#
# license:   This file is licensed under the GPLv2
#            For more informations read:
#            http://www.gnu.org/licenses/gpl-2.0.txt
#
# part of:   This file is part of the XazeInstaller
#
###############################################################################



"""
Packages management for a config section
"""



import lxml.etree
import utils



class Structure:

    def __init__(self, init_space, pkgs, schema, configdir, currentuser):
        """
        Initialize the Structure management object
        Parameters:
            1- init_space: number of spaces before each 'print'
            2- pkgs: Path of the xml file that contains the pkgs
            3- schema: Path of the XMLSchema file, used for the validation
                       of the struct file
            4- configdir: Path of the config section directory
            5- currentuser: Current logged in user
        """

        self.currentuser = currentuser

        self.fileinfo = utils.FileInfo(self.currentuser)

        # config section directory
        self.configdir = configdir

        # parse the input xml file 'schema' and build the parser
        #     to validate 'pkgs'
        xschema = lxml.etree.XMLSchema(lxml.etree.parse(schema))
        parser = lxml.etree.XMLParser(schema = xschema)

        # parse and validate 'struct' with the 'parser'
        self.pkgs_root = lxml.etree.parse(pkgs, parser)
        print "%s>>> Validated %s with %s" % (init_space, pkgs, schema)


    def __str__(self):
        return lxml.etree.tostring(self.pkgs_root)

    def __repr__(self):
        return repr(self.pkgs_root)


    def _clean_string(self, string, add_patterns=[]):
        """
        Remove from the input 'string' all of the characters in the
        patterns list.
        The pattern list is pre-built, but it can be extended with the
        optional list argument 'add_patterns'
        """

        # 'patterns' list contains all of the strings that will be removed
        #     from the input 'string'
        patterns = ["\a", "\b", "\f", "\n", "\t", "\v"]
        if len(add_patterns) > 0:
            patterns.extend(add_patterns)
        
        # remove from the input 'string' the matched elements in 'patterns'
        for pattern in patterns:
            string = string.replace(pattern, "")
        # remove all spaces
        string = string.replace(" ", "")
        
        return string


    def process(self, process_space):

        for item in self.struct_root.iterfind("//pkg"):

            # get the package name for the current 'pkg'
            package = item.text






if __name__ == "__main__":
    print "struct executed"
